<?php
/*
    
    $query='
        Select  link 
        From    `'.$config['mysql']['database'].'`.`Users` AS Users
                Left Join (`'.$config['mysql']['database'].'`.`UserSkins` AS UserSkins, `'.$config['mysql']['database'].'`.`Skins` AS Skins)
                On (UserSkins.userId = Users.id AND UserSkins.skinId = Skins.id)
        Where   Users.username = "'.$_GET['name'].'";
    ';
    
    */

    require_once "../scripts/config.php";
    require_once "../scripts/functions.php";
    
    /* Query to select skin links for this particular player */
    $query = '
        Select  link
        From    `'.$config['mysql']['database'].'`.`ActiveSkin` AS ActiveSkin
                Left Join (`'.$config['mysql']['database'].'`.`Users` AS Users, `'.$config['mysql']['database'].'`.`Skins` AS Skins)
                On (ActiveSkin.userId = Users.id AND ActiveSkin.skinId = Skins.id)
        Where   Users.username = "'.$_GET['name'].'";
    ';
    
    $resource = runQuery($query);
    $rownum = mysql_num_rows($resource);
    
    if($rownum > 0){
        $records = array();
        while($record = mysql_fetch_assoc($resource)){
            array_push($records, $record);
        }
        
        
        if($rownum == 1){
            $file = $records[0]['link'];
        } else {
            srand(time());
            $file = $records[rand(0, count($records)-1)]['link'];
        }
    } else{
        $file = "skins/default.png";
    }
    
    header('Content-Type: image/png');
    header('Content-Length:'.filesize($file));
    header('Content-Disposition: attachment;filename="'.$_GET['name'].'"');
    
    $fp=fopen($file,'r');
    fpassthru($fp);
    
    fclose($fp);
?>